var util = require('../../utils/util.js');
var qiniuUploader = require('../../utils/qiniuUploader.js');
var WXBizDataCrypt = require('../../crypto/WXBizDataCrypt.js');
let app = getApp();
let imgUrl = app.imgUrl;
let url = app.url;
let gobalData = app.gobalData;
Page({
	data: {
		userAvatar: '',
		sendContent: '',
		images: [],
		labelList: [],
		submitFlag: false,
		submitLoading: false
	},
	//事件处理函数
	onLoad: function() {
		util(`${url}ArticleCate/getArticleCate`).then(res => {
			if (res.status == 1) {
				this.setData({
					labelList: res.content.data
				})
			}
		})
	},
	// 提交表单
	formSubmit: function(e) {
		let self = this;
		this.setData({
			submitLoading: true
		})
		if (this.data.submitFlag == false) {
			let arr = [];
			let cid = wx.getStorageSync('userInfo').userInfo.id;
			let label = e.detail.value.checkedBox;
			let content = encodeURI(e.detail.value.sendContent);
			let obj = {
				cid,
				label,
				content
			};
			if (content == "") {
				this.setData({
					submitFlag: false,
					submitLoading: false
				})
				wx.showToast({
					title: "你还没有输入内容！",
					icon: "none"
				})
				return false;
			}
			if (label.length >= 4) {
				wx.showToast({
					title: "标签个数请不要超过3个",
					icon: "none"
				})
				return false;
			}

			let upLoad = (img) => {
				return new Promise((resolve, reject) => {
					qiniuUploader.upload(img, (res) => {
						resolve(res.imageURL);
					}, (error) => {
						reject(error)
					}, {
						domain: imgUrl,
						region: 'ECN',
						uptokenURL: `${url}Upload/getUpToken`
					})
				})
			}

			if (self.data.images.length >= 1) {
				wx.showLoading({
					title: '正在疯狂上传中...'
				})
				let imgI = 0
				function myFor () {
					if (imgI < self.data.images.length) {
						upLoad(self.data.images[imgI]).then(res => {
							// 处理图片上传过程
								arr.push(res)
								imgI++
								return myFor()
						})
					} else {
						// 图片已经上传成功
						obj.pictureArray = arr
						util(`${url}Article/addArticle`, obj).then(res => {
							// console.log(obj, '=====提交的数据');
							if (res.status == 1) {
								wx.hideLoading()
								// 如果提交成功应该跳转到装修圈主页面比较好
								wx.navigateBack({
									url: "/pages/decorationRing/index"
								})
							} else {
								wx.hideLoading()
								this.setData({
									submitFlag: false,
									submitLoading: false
								})
								wx.showToast({
									title: res.msg,
									icon: "none"
								})
							}
						}).catch(err => {
							wx.hideLoading()
							console.log(err)
						})
						return
					}
				}
				myFor()
// 				for (let i = 0; i < self.data.images.length; i++) {
// 					upLoad(self.data.images[i]).then(res => {
// 							arr.push(res);
// 							if (i == self.data.images.length - 1) {
// 								setTimeout(() => {
// 									obj.pictureArray = arr;
// 									util(`${url}Article/addArticle`, obj).then(res => {
// 										// console.log(obj, '=====提交的数据');
// 										if (res.status == 1) {
// 											wx.hideLoading()
// 											// 如果提交成功应该跳转到装修圈主页面比较好
// 											wx.navigateBack({
// 												url: "/pages/decorationRing/index"
// 											})
// 										} else {
// 											wx.hideLoading()
// 											this.setData({
// 												submitFlag: false,
// 												submitLoading: false
// 											})
// 											wx.showToast({
// 												title: res.msg,
// 												icon: "none"
// 											})
// 										}
// 									})
// 								}, 2000)
// 							}
// 						})
// 						.catch(err => {
// 							console.log(err)
// 						})
// 				}
			} else {
				util(`${url}Article/addArticle`, obj).then(res => {
					if (res.status == 1) {
						// 如果提交成功应该跳转到装修圈主页面比较好
						wx.navigateBack({
							url: "/pages/decorationRing/index"
						})
					} else {
						this.setData({
							submitFlag: false,
							submitLoading: false
						})
						wx.showToast({
							title: res.msg,
							icon: "none"
						})
					}
				})
			}
		} else {
			return false;
		}
	},

	// 图片展示
	chooseImage(e) {
		wx.chooseImage({
			count: 9,
			sizeType: ['original', 'compressed'], //可选择原图或压缩后的图片
			sourceType: ['album', 'camera'], //可选择性开放访问相册、相机
			success: res => {
				const images = res.tempFilePaths;
				this.setData({
					images: images
				})
			}
		})
	},
	// 判断用户选择删除还是预览
	handleImagePreview(e) {
		wx.showModal({
			title: '删除，预览',
			content: '删除当前图片，预览当前图片',
			cancelText: '删除',
			cancelColor: '#fa863d',
			confirmText: '预览',
			confirmColor: '#fa863d',
			success: res => {
				// 预览
				if (res.confirm) {
					const idx = e.target.dataset.idx
					const images = this.data.images
					wx.previewImage({
						current: images[idx], //当前预览的图片
						urls: images, //所有要预览的图片
					})
				} else { //删除
					const idx = e.target.dataset.idx
					this.data.images.splice(idx, 1);
					this.setData({
						images: this.data.images
					})
				}
			}
		})
	},
	// 多选按钮点击变色
	clicks: function(e) {
		let index = e.currentTarget.dataset.index; //获取你点击的索引下标
		let arrs = this.data.labelList;
		if (arrs[index].status == 1) {
			arrs[index].status = 0;
		} else {
			arrs[index].status = 1;
		}
		this.setData({
			labelList: arrs
		})
	}
})
